package com.jlrdb.controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.jlrdb.model.CellularBroadcast;
import com.jlrdb.model.CellularBroadcasts;
import com.jlrdb.service.CellularBroadcastService;

@SuppressWarnings("serial")
public class CellularBroadcastServlet extends HttpServlet {
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		CellularBroadcastService cellularBroadcastService = new CellularBroadcastService();
		if (action.equals("viewCellularChoices"))
		{
			UserService userService = UserServiceFactory.getUserService();
			User user = userService.getCurrentUser();
			String Role;
			String domain = "temp";
			String identity = user.getEmail();
			if (identity.contains("jaguarlandrover.com"))
				domain = "jaguarlandrover.com";
			if (domain.equalsIgnoreCase("jaguarlandrover.com"))
			{
				if (identity.equalsIgnoreCase("gagarwa2@jaguarlandrover.com") || identity.equalsIgnoreCase("rholenar@jaguarlandrover.com") || identity.equalsIgnoreCase("poreill3@jaguarlandrover.com")
						|| identity.equalsIgnoreCase("sarcot@jaguarlandrover.com"))
					Role = "Admin";
				else
					Role = "Viewer";
				HttpSession session = request.getSession();
				session.setAttribute("Role", Role);
				RequestDispatcher rd = request.getRequestDispatcher("jsp/viewCellularBroadcastByChoice.jsp");
				rd.forward(request, response);
			} else
			{
				String message = "Please LOGIN with a valid JLR domain account.";
				request.setAttribute("message", message);
				RequestDispatcher rd = request.getRequestDispatcher("jsp/message.jsp");
				rd.forward(request, response);
			}
		} else if (action.equals("addCellularBroadcast"))
		{
			CellularBroadcast cellularBroadcast = new CellularBroadcast();
			cellularBroadcast.setCountry(request.getParameter("country"));
			cellularBroadcast.setGeographic_Region(request.getParameter("geographic_region"));
			cellularBroadcast.setCellular_Technology(request.getParameter("cellular_technology"));
			cellularBroadcast.setData_Enhancement_Technology(request.getParameter("data_enhancement_technology"));
			cellularBroadcast.setInformation_Transfer_Rate(request.getParameter("information_transfer_rate:"));
			cellularBroadcast.setStart_Date(request.getParameter("start_date"));
			cellularBroadcast.setEnd_Date(request.getParameter("end_date"));
			cellularBroadcast.setFrequency_Spectrum(request.getParameter("frequency_spectrum"));
			cellularBroadcast.setMarket_Penetration(request.getParameter("market_penetration"));
			cellularBroadcast.setOperators(request.getParameter("operators"));
			cellularBroadcast.setSource_Of_Reference(request.getParameter("source_of_reference"));
			cellularBroadcast.setAditional_Contacts(request.getParameter("aditional_contacts"));
			cellularBroadcast.setIndustry_Regulators(request.getParameter("industry_regulators"));
			cellularBroadcast.setMarket_Confirmation(request.getParameter("market_confirmation"));
			cellularBroadcast.setNotes(request.getParameter("notes"));
			cellularBroadcast.setProblems(request.getParameter("problems"));
			cellularBroadcast = cellularBroadcastService.checkEmptyValues(cellularBroadcast);
			int result = cellularBroadcastService.addCellularBroadcast(cellularBroadcast);
			if (result == 0)
			{
				request.setAttribute("cellularBroadcast", cellularBroadcast);
				RequestDispatcher rd = request.getRequestDispatcher("jsp/cellularBroadcastAdded.jsp");
				rd.forward(request, response);
			}
		} else if (action.equals("viewCellularBroadcasts"))
		{
			CellularBroadcasts cellularBroadcasts = new CellularBroadcasts();
			String geographic_region = request.getParameter("geographic_region");
			String country = request.getParameter("country");
			String cellular_technology = request.getParameter("cellular_technology");
			String market_confirmation = request.getParameter("market_confirmation");
			CellularBroadcast cellularQuery = new CellularBroadcast();
			cellularQuery.setCountry(country);
			cellularQuery.setGeographic_Region(geographic_region);
			cellularQuery.setCellular_Technology(cellular_technology);
			cellularQuery.setMarket_Confirmation(market_confirmation);
			cellularBroadcasts = cellularBroadcastService.viewCellularBroadcasts(cellularQuery);
			if (cellularBroadcasts.getCellularBroadcasts().size() == 0)
			{
				String message = "Sorry no data available for following criteria:";
				if (geographic_region.length() != 0)
					message += "\n geographic_region: " + geographic_region;
				if (country.length() != 0)
					message += "\n country: " + country;
				if (cellular_technology.length() != 0)
					message += "\n dtt_standard: " + cellular_technology;
				if (market_confirmation.length() != 0)
					message += "\n dtt_status: " + market_confirmation;
				request.setAttribute("message", message);
				RequestDispatcher rd = request.getRequestDispatcher("jsp/message.jsp");
				rd.forward(request, response);
			} else if (country.length() != 0)
			{
				request.setAttribute("cellularBroadcasts", cellularBroadcasts);
				RequestDispatcher rd = request.getRequestDispatcher("jsp/viewCellularFullDetails.jsp");
				rd.forward(request, response);
			} else
			{
				if (geographic_region.length() != 0)
					request.setAttribute("geographic_region", geographic_region);
				else
					request.setAttribute("geographic_region", "none");
				request.setAttribute("cellularBroadcasts", cellularBroadcasts);
				RequestDispatcher rd = request.getRequestDispatcher("jsp/viewCellularBroadcasts.jsp");
				rd.forward(request, response);
			}
		} else if (action.equals("getThisBroadcast"))
		{
			long broadcastId = Long.parseLong(request.getParameter("broadcastId"));
			CellularBroadcast cellularBroadcast = new CellularBroadcast();
			cellularBroadcast = cellularBroadcastService.getThisBroadcast(broadcastId);
			request.setAttribute("cellularBroadcast", cellularBroadcast);
			RequestDispatcher rd = request.getRequestDispatcher("jsp/updateCellularBroadcast.jsp");
			rd.forward(request, response);
		} else if (action.equals("updateCellularBroadcast"))
		{
			CellularBroadcast cellularBroadcast = new CellularBroadcast();
			cellularBroadcast.setCountry(request.getParameter("country"));
			// cellularBroadcast.setGeographic_Region(request.getParameter("geographic_region"));
			cellularBroadcast.setCellular_Technology(request.getParameter("cellular_technology"));
			cellularBroadcast.setData_Enhancement_Technology(request.getParameter("data_enhancement_technology"));
			cellularBroadcast.setInformation_Transfer_Rate(request.getParameter("information_transfer_rate:"));
			cellularBroadcast.setStart_Date(request.getParameter("start_date"));
			cellularBroadcast.setEnd_Date(request.getParameter("end_date"));
			cellularBroadcast.setFrequency_Spectrum(request.getParameter("frequency_spectrum"));
			cellularBroadcast.setMarket_Penetration(request.getParameter("market_penetration"));
			cellularBroadcast.setOperators(request.getParameter("operators"));
			cellularBroadcast.setSource_Of_Reference(request.getParameter("source_of_reference"));
			cellularBroadcast.setAditional_Contacts(request.getParameter("aditional_contacts"));
			cellularBroadcast.setIndustry_Regulators(request.getParameter("industry_regulators"));
			cellularBroadcast.setMarket_Confirmation(request.getParameter("market_confirmation"));
			cellularBroadcast.setNotes(request.getParameter("notes"));
			cellularBroadcast.setProblems(request.getParameter("problems"));
			DateFormat df = new SimpleDateFormat("dd/MM/yy");
			String formattedDate = df.format(new Date());
			cellularBroadcast.setLast_Date_Verified(formattedDate);
			long broadcastId = Long.parseLong(request.getParameter("broadcastId"));
			cellularBroadcast = cellularBroadcastService.checkEmptyValues(cellularBroadcast);
			int result = cellularBroadcastService.updateCellularBroadcast(cellularBroadcast, broadcastId);
			if (result == 0)
			{
				CellularBroadcasts cellularBroadcasts = new CellularBroadcasts();
				String country = request.getParameter("country");
				CellularBroadcast cellularQuery = new CellularBroadcast();
				cellularQuery.setCountry(country);
				cellularBroadcasts = cellularBroadcastService.viewCellularBroadcasts(cellularQuery);
				request.setAttribute("cellularBroadcasts", cellularBroadcasts);
				RequestDispatcher rd = request.getRequestDispatcher("jsp/viewCellularFullDetails.jsp");
				rd.forward(request, response);
			}
		}
	}
}
